

let baseUtil = {};



/**
 * 页面刷新
 */
baseUtil.pageRefresh = function () {
    if (baseUtil.isWeiXin() && baseUtil.isAndroid()) {
        let time = new Date().getTime();
        let currentUrl = window.location.href;
        window.location.href = currentUrl + (currentUrl.indexOf('?') == -1 ? '?' : '&') + 't=' + time;
    } else {
        window.location.reload();
    }
};

/**
 * 表单重置
 */

baseUtil.handleReset=function(that,name){
    that.$refs[name].resetFields();
};


/* 随机数 */
baseUtil.randomString = function(len){
    len = len || 32;
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var maxPos = chars.length;
    var pwd = '';
    for (let i = 0; i < len; i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

baseUtil.setSessionStorage = function (key, value) {
    try {
        sessionStorage.setItem(key, value);
    } catch (e) {
        console.log('不支持sessionStorage');
    }
};

baseUtil.getSessionStorage = function (key) {
    let value = '';
    try {
        value = sessionStorage.getItem(key);
    } catch (e) {
        console.log('不支持sessionStorage');
    }
    return value;
};

baseUtil.removeSessionStorage = function (key) {
    try {
        sessionStorage.removeItem(key);
    } catch (e) {
        console.log('不支持sessionStorage');
    }
};

baseUtil.setLocalStorage = function (key, value) {
    try {
        localStorage.setItem(key, value);
    } catch (e) {
        console.log('不支持localStorage');
    }
};

baseUtil.getLocalStorage = function (key) {
    let value = '';
    try {
        value = localStorage.getItem(key);
    } catch (e) {
        console.log('不支持localStorage');
    }
    return value;
};

baseUtil.removeLocalStorage = function (key) {
    try {
        localStorage.removeItem(key);
    } catch (e) {
        console.log('不支持localStorage');
    }
};

baseUtil.jsonToString = function (obj) {
    try {
        return JSON.stringify(obj);
    } catch (e) {
        console.log('json转换错误');
        return '';
    }
};

baseUtil.stringToJson = function (str) {
    try {
        return JSON.parse(str);
    } catch (e) {
        console.log('json解析错误');
        return '';
    }
};

baseUtil.pageJump =function(that,id,url){
    //跳转
    let route = '';
    if (id.length > 0) {
        route = {
            path: url,
            query: {
                id: id
            },
        };
    } else {
        if(id > 0){
            route = {
                path: url,
                query: {
                    id: id
                },
            };
        }else {
            route = {
                path: url,
            };
        }

    }

    that.$router.push(route);
};
//数组转','隔开的字符串
baseUtil.textByJquery =function (arr) {
    let imgStr = '';
    let i = 0;
    for (i = 0; i < arr.length; i++) {
        imgStr+=  arr[i] + ',';
    }
    if (imgStr.length > 0) {

        imgStr = imgStr.substr(0,imgStr.length - 1);

    }
    return imgStr;
}
//存cookie
baseUtil.setCookie =function (name,value) {
    let Days = 30;
    let exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}


//取cookie
baseUtil.getCookie =function (name,value) {
    let  arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
baseUtil.version = '1.0.0';

export default baseUtil;
